﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using LotteryVote.Data;
using LotteryVote.Utility;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote.Logic
{
    public class BetLimitFacade
    {
        private BetLimitDataAccess _daBetLimit;
        internal BetLimitDataAccess DaBetLimit
        {
            get
            {
                if (_daBetLimit == null)
                    _daBetLimit = new BetLimitDataAccess();
                return _daBetLimit;
            }
            set
            {
                _daBetLimit = value;
            }
        }

        private GameBetLimitDataAccess _daGameBetLimit;
        internal GameBetLimitDataAccess DaGameBetLimit
        {
            get
            {
                if (_daGameBetLimit == null)
                    _daGameBetLimit = new GameBetLimitDataAccess();
                return _daGameBetLimit;
            }
            set
            {
                _daGameBetLimit = value;
            }
        }

        private UserFacade _blUser;
        public UserFacade BlUser
        {
            get
            {
                if (_blUser == null)
                    _blUser = new UserFacade();
                return _blUser;
            }
            set
            {
                _blUser = value;
            }
        }

        public void AddGameBetLimit(GameBetLimit limit)
        {
            DaGameBetLimit.Insert(limit);
        }

        public void UpdateBetLimit(int userId, IList<BetLimit> betLimitList)
        {
            //TODO:还需检查下属是否有用户，有用户情况下减少限制仔细考虑
            var user = BlUser.GetUser(userId);
            if (user.Role > SystemRole.Company)
            {
                var parentLimitList = DaBetLimit.GetParentLimit(userId);
                if (parentLimitList == null || parentLimitList.Count == 0)
                    throw new InvalidDataException("用户限制数据错误!ChildUserId:" + userId);
                betLimitList.ForEach(it =>
                {
                    var parentLimit = parentLimitList.Find(x => x.GameId == it.GameId);
                    if (parentLimit == null)
                        throw new InvalidDataException("用户限制数据错误!ChildUserId:" + userId + " GameId:" + it.GameId);
                    if (it.LeastLimit > parentLimit.LeastLimit || it.LargestLimit > parentLimit.LargestLimit)
                        throw new PromptException("Please make sure that the user's limit ture!");
                });
            }
            DaBetLimit.ExecuteWithTransaction(() =>
            {
                DaBetLimit.Delete(userId);
                betLimitList.ForEach(it =>
                {
                    DaBetLimit.Insert(it);
                });
            });
        }
        public void UpdateGameBetLimit(int userId, CompanyType companyType, IList<GameBetLimit> betLimitList)
        {
            var parentGameLimitList = DaGameBetLimit.GetParentGameLimit(userId, companyType);
            if (parentGameLimitList == null || parentGameLimitList.Count == 0)
            {
                throw new InvalidDataException("游戏限制数据异常!ChildUserId:" + userId);
            }
            betLimitList.ForEach(it =>
            {
                var limit = parentGameLimitList.Find(x => x.GamePlayWayId == it.GamePlayWayId);
                if (limit == null)
                    throw new InvalidDataException("游戏限制数据错误!ChildUserId:" + userId + " GamePlayWayId:" + it.GamePlayWayId);
                if (it.LimitValue > limit.LimitValue)
                    throw new PromptException("Please make sure that the game's limit ture!");
            });

            DaGameBetLimit.ExecuteWithTransaction(() =>
            {
                DaGameBetLimit.Delete(userId, (int)companyType);
                betLimitList.ForEach(it =>
                {
                    it.UserId = userId;
                    DaGameBetLimit.Insert(it);
                });
            });
        }
        public void UpdateGmeBetLimit(GameBetLimit limit)
        {
            bool isCompany = false;
            var parentGameLimitList = DaGameBetLimit.GetParentGameLimit(limit.UserId, limit.CompanyType);
            if (parentGameLimitList == null || parentGameLimitList.Count == 0)
            {
                var user = BlUser.GetUser(limit.UserId);
                if (user.Role != SystemRole.Company)
                    throw new InvalidDataException("游戏限制数据异常!ChildUserId:" + limit.UserId);
                isCompany = true;
            }

            if (!isCompany)
            {
                var parentLimit = parentGameLimitList.Find(x => x.GamePlayWayId == limit.GamePlayWayId);
                if (parentLimit == null)
                    throw new InvalidDataException("游戏限制数据错误!ChildUserId:" + limit.UserId + " GamePlayWayId:" + limit.GamePlayWayId);
                if (limit.LimitValue > parentLimit.LimitValue)
                    throw new PromptException("Please make sure that the game's limit ture!");
            }
            DaGameBetLimit.Insert(limit);
        }
        public void UpdateAllUserGameBetLimit(int gameplaywayId, IList<GameBetLimit> betLimitList)
        {
            DaGameBetLimit.Clear(gameplaywayId);
            var allUser = BlUser.GetAllUser();
            allUser.ForEach(user =>
            {
                foreach (var limitItem in betLimitList)
                {
                    limitItem.UserId = user.UserId;
                    UpdateGmeBetLimit(limitItem);
                }
            });
        }

        public IList<GameBetLimit> ListGameLimitByUser(int userId)
        {
            return DaGameBetLimit.ListLimitByUser(userId);
        }
        public IList<BetLimit> ListLimitByUser(int userId)
        {
            return DaBetLimit.ListLimitByUser(userId);
        }
    }
}
